package com.crl.gen.controller;

import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.crl.gen.dto.GenFieldDTO;
import com.crl.gen.model.GenField;
import com.crl.gen.service.GenCreateService;
import com.crl.gen.service.GenDbFieldService;
import com.crl.gen.vo.GenCreateSearchVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/25 16:52
 */
@RestController
@RequestMapping("/gen/create")
@Api(tags = "代码生成器-生成代码")
public class GenCreateController {
    @Autowired
    private GenCreateService genCreateService;
    @Autowired
    private GenDbFieldService genDbFieldService;

    /**
     * 判断文件是否存在
     *
     * @return
     */
    @RequestMapping("/checkFileExist")
    public JsonResult checkFileExist(@RequestBody GenCreateSearchVO genCreateSearchVO) {
        if (genCreateSearchVO.getType() == 0) {//全部判断
            String str = "";
            boolean flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), 1);
            if (flag) {
                str += "Model文件已存在；";
            }
            flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), 2);
            if (flag) {
                str += "VO文件已存在；";
            }
            flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), 3);
            if (flag) {
                str += "DTO文件已存在；";
            }
            flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), 4);
            if (flag) {
                str += "Dao文件已存在；";
            }
            flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), 5);
            if (flag) {
                str += "Service文件已存在；";
            }
            flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), 6);
            if (flag) {
                str += "Controller文件已存在；";
            }
            return JsonResultUtil.ok(str);
        } else {
            boolean flag = genCreateService.checkFileExist(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), genCreateSearchVO.getType());
            if (flag) {
                return JsonResultUtil.ok("文件已存在");
            } else {
                return JsonResultUtil.ok("");
            }
        }
    }

    /**
     * 生成代码
     *
     * @return
     */
    @RequestMapping("/createCode")
    public JsonResult createCode(@RequestBody GenCreateSearchVO genCreateSearchVO) {
        String code = genCreateService.createCode(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId(), genCreateSearchVO.getType());
        return JsonResultUtil.ok(code);
    }

    /**
     * 生成文件
     *
     * @return
     */
    @RequestMapping("/createFile")
    public JsonResult createFile(@RequestBody GenCreateSearchVO genCreateSearchVO) {
        if (genCreateSearchVO.getType() == 0) {//全部生成
            genCreateSearchVO.setType(1);
            genCreateService.createFile(genCreateSearchVO);
            genCreateSearchVO.setType(2);
            genCreateService.createFile(genCreateSearchVO);
            genCreateSearchVO.setType(3);
            genCreateService.createFile(genCreateSearchVO);
            genCreateSearchVO.setType(4);
            genCreateService.createFile(genCreateSearchVO);
            genCreateSearchVO.setType(5);
            genCreateService.createFile(genCreateSearchVO);
            genCreateSearchVO.setType(6);
            genCreateService.createFile(genCreateSearchVO);
        } else {
            genCreateService.createFile(genCreateSearchVO);
        }
        return JsonResultUtil.ok();
    }

    /**
     * 生成Sql代码
     *
     * @return
     */
    @RequestMapping("/genSqlCode")
    public JsonResult genSqlCode(@RequestBody GenCreateSearchVO genCreateSearchVO) {
        String code = genCreateService.createTableSql(genCreateSearchVO.getAuthorId(), genCreateSearchVO.getTableId());
        return JsonResultUtil.ok(code);
    }

    /**
     * 根据表名加载数据字段
     *
     * @param map
     * @return
     */
    @RequestMapping("/loadDbField")
    public JsonResult loadDbField(@RequestBody Map<String, Integer> map) {
        List<GenField> list = genDbFieldService.loadDbField(map.get("tableId"));
        return JsonResultUtil.ok(list);
    }

    /**
     * 保存数据表配置
     *
     * @param list
     * @return
     */
    @RequestMapping("/saveDbField")
    public JsonResult saveDbField(@RequestBody List<GenFieldDTO> list) {
        if (list.size() == 0) {
            return JsonResultUtil.error("字段为空");
        } else {
            genDbFieldService.saveDbFileld(list);
            return JsonResultUtil.ok();
        }
    }
}
